package com.rs.net.decoders;

import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import com.rs.Settings;
import com.rs.cache.loaders.ObjectDefinitions;
import com.rs.game.Animation;
import com.rs.game.Graphics;
import com.rs.game.Hit;
import com.rs.game.Hit.HitLook;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.FloorItem;
import com.rs.game.item.Item;
import com.rs.game.npc.NPC;
import com.rs.game.player.CoordsEvent;
import com.rs.game.player.Equipment;
import com.rs.game.player.ForceMovement;
import com.rs.game.player.Player;
import com.rs.game.player.PublicChatMessage;
import com.rs.game.player.QuickChatMessage;
import com.rs.game.player.Skills;
import com.rs.game.player.content.Magic;
import com.rs.game.player.skills.Combat;
import com.rs.game.player.skills.Woodcutting;
import com.rs.game.player.skills.Woodcutting.TreeDefinitions;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.io.InputStream;
import com.rs.net.Session;
import com.rs.utils.Logger;
import com.rs.utils.Utils;
import com.rs.utils.huffman.Huffman;
import com.rs.game.player.skills.Mining;
import com.rs.game.player.skills.Runecrafting;

public final class WorldPacketsDecoder extends Decoder {

	private static final byte[] PACKET_SIZES = new byte[256];
	
	private final static int WALKING_PACKET = 119;
	private final static int MINI_WALKING_PACKET = 163;
	private final static int WEIRD_WALKING_PACKET = 160;
	private final static int AFK_PACKET = 244;
	private final static int ACTION_BUTTON1_PACKET = 216;
	private final static int ACTION_BUTTON2_PACKET = 205;
	private final static int WEAR_ITEM_PACKET = 229;
	private final static int ACTION_BUTTON4_PACKET = 19;
	private final static int ACTION_BUTTON5_PACKET = 193;
	private final static int ACTION_BUTTON6_PACKET = 76;
	private final static int ACTION_BUTTON7_PACKET = 173;
	private final static int ACTION_BUTTON8_PACKET = 89;
	private final static int ACTION_BUTTON9_PACKET = 221;
	private final static int PLAYER_TRADE_OPTION_PACKET = 141;
	private final static int MOVE_CAMERA_PACKET = 235;
	private final static int CLICK_PACKET = 87;
	private final static int CLOSE_INTERFACE_PACKET = 91;
	private final static int COMMANDS_PACKET = 171;
	private final static int IN_OUT_SCREEN_PACKET = 4;
	private final static int INTER_PACKET_COUNT_CHECKER_PACKET = 5;
	private final static int SWITCH_DETAIL = 155;
	private final static int DONE_LOADING_REGION = 116;
	private final static int PING_PACKET = 255;
	private final static int SCREEN_PACKET = 170;
	private final static int PUBLIC_CHAT_PACKET = 182;
	private final static int QUICK_CHAT_PACKET = 68;
	private final static int ADD_FRIEND_PACKET = 226;
	private final static int REMOVE_FRIEND_PACKET = 92;
	private final static int SEND_FRIEND_MESSAGE_PACKET = 123;
	private final static int OBJECT_CLICK1_PACKET = 45;
	private final static int OBJECT_CLICK2_PACKET = 190;
	private final static int OBJECT_CLICK3_PACKET = 26;
	private final static int NPC_CLICK1_PACKET = 217;
	private final static int NPC_CLICK2_PACKET = 254;
	private final static int ATTACK_NPC = 207;
	private final static int ATTACK_PLAYER = 152;
	private final static int ITEM_DROP_PACKET = 248;
	private final static int ITEM_TAKE_PACKET = 194;
	private final static int DIALOGUE_CONTINUE_PACKET = 147;
	private final static int ENTER_INTEGER_PACKET = 206;
	private final static int SWITCH_INTERFACE_ITEM_PACKET = 253;
	private final static int ITEM_OPTION1_PACKET = 234;
	private final static int INTERFACE_ON_PLAYER = 79;
	private final static int INTERFACE_ON_NPC = 84;
	private final static int ITEM_ON_OBJECT = 202;
	static {
		loadPacketSizes();
	}
	
	public static void loadPacketSizes() {
		for(int id = 0; id < 256; id++)
			PACKET_SIZES[id] = -4;
		PACKET_SIZES[AFK_PACKET] = 0;
		PACKET_SIZES[ITEM_ON_OBJECT] = 15;
		PACKET_SIZES[ACTION_BUTTON1_PACKET] = 6;
		PACKET_SIZES[WEIRD_WALKING_PACKET] = 4;
		PACKET_SIZES[ACTION_BUTTON2_PACKET] = 4;
		PACKET_SIZES[WEAR_ITEM_PACKET] = 8;
		PACKET_SIZES[ACTION_BUTTON4_PACKET] = 6;
		PACKET_SIZES[ACTION_BUTTON5_PACKET] = 6;
		PACKET_SIZES[ACTION_BUTTON6_PACKET] = 6;
		PACKET_SIZES[ACTION_BUTTON7_PACKET] = 6;
		PACKET_SIZES[ACTION_BUTTON8_PACKET] = 6;
		PACKET_SIZES[ACTION_BUTTON9_PACKET] = 6;
		PACKET_SIZES[CLICK_PACKET] = 6;
		PACKET_SIZES[CLOSE_INTERFACE_PACKET] = 0;
		PACKET_SIZES[COMMANDS_PACKET] = -1;
		PACKET_SIZES[IN_OUT_SCREEN_PACKET] = 1;
		PACKET_SIZES[INTER_PACKET_COUNT_CHECKER_PACKET] = 2;
		PACKET_SIZES[PING_PACKET] = 0;
		PACKET_SIZES[MINI_WALKING_PACKET] = -1;
		PACKET_SIZES[WALKING_PACKET] = -1;
		PACKET_SIZES[SCREEN_PACKET] = 6;
		PACKET_SIZES[SWITCH_DETAIL] = 4;
		PACKET_SIZES[DONE_LOADING_REGION] = 0;
		PACKET_SIZES[PUBLIC_CHAT_PACKET] = -1;
		PACKET_SIZES[QUICK_CHAT_PACKET] = -1;
		PACKET_SIZES[ADD_FRIEND_PACKET] = -1;
		PACKET_SIZES[REMOVE_FRIEND_PACKET] = -1;
		PACKET_SIZES[SEND_FRIEND_MESSAGE_PACKET] = -1;
		PACKET_SIZES[OBJECT_CLICK1_PACKET] = 7;
		PACKET_SIZES[OBJECT_CLICK2_PACKET] = 7;
		PACKET_SIZES[OBJECT_CLICK3_PACKET] = 6;
		PACKET_SIZES[ITEM_DROP_PACKET] = 8;
		PACKET_SIZES[ITEM_TAKE_PACKET] = 7;
		PACKET_SIZES[DIALOGUE_CONTINUE_PACKET] = 6;
		PACKET_SIZES[MOVE_CAMERA_PACKET] = 4;
		PACKET_SIZES[ENTER_INTEGER_PACKET] = 4;
		PACKET_SIZES[PLAYER_TRADE_OPTION_PACKET] = 3;
		PACKET_SIZES[SWITCH_INTERFACE_ITEM_PACKET] = 9;
		PACKET_SIZES[ATTACK_NPC] = 3;
		PACKET_SIZES[ATTACK_PLAYER] = 3;
		PACKET_SIZES[NPC_CLICK1_PACKET] = 3;
		PACKET_SIZES[NPC_CLICK2_PACKET] = 3;
		PACKET_SIZES[ITEM_OPTION1_PACKET] = 8;
		PACKET_SIZES[INTERFACE_ON_PLAYER] = 9;
		PACKET_SIZES[INTERFACE_ON_NPC] = 9;
	}
	
	private Player player;
	private boolean clicked;
	public WorldPacketsDecoder(Session session, Player player) {
		super(session);
		this.player = player;
	}

	@Override
	public void decode(InputStream stream) {
		while (stream.getRemaining() > 0 && session.getChannel().isConnected() && !player.hasFinished()) {
			int packetId = stream.readUnsignedByte();
			if(packetId >= PACKET_SIZES.length) {
				System.out.println("PacketId " +packetId+ " has fake packet id.");
				break;
			}
			int length = PACKET_SIZES[packetId];
			if (length == -1)
				length = stream.readUnsignedByte();
			else if (length == -2)
				length = stream.readUnsignedShort();
			else if (length == -3)
				length = stream.readInt();
			else if (length == -4) {
				length = stream.getRemaining();
				System.out.println("Invalid size for PacketId "+packetId+". Size guessed to be "+length);
			}
			if(length > stream.getRemaining()) {
				length = stream.getRemaining();
				System.out.println("PacketId " +packetId+ " has fake size. - expected size " +length);
				//break;
				
			}
		//	System.out.println("PacketId " +packetId+ " has . - expected size " +length);
			int startOffset = stream.getOffset();
			processPackets(packetId, stream, length);
			stream.setOffset(startOffset + length);
		}
	}

	public void processPackets(final int packetId, InputStream stream, int length) {
		player.setPacketsDecoderPing(System.currentTimeMillis());
		if(packetId == PING_PACKET) {
			//kk we ping :)
		}else if(packetId == AFK_PACKET) {
			player.getSession().getChannel().close();
		}else if(packetId == CLOSE_INTERFACE_PACKET) {
			if(!player.isRunning()) {
				player.run();
				return;
			}
			player.stopAll();
		}else if(packetId == MOVE_CAMERA_PACKET) {
			//not using it atm
			stream.readShort();
			stream.readShortLE();
		}else if(packetId == INTER_PACKET_COUNT_CHECKER_PACKET) {
			if(stream.readUnsignedShort() != player.getPackets().getInterPacketsCount()) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				//may miss
			//	player.getPackets().sendGameMessage("Invalid interface packet count.");
			}
		}else if(packetId == IN_OUT_SCREEN_PACKET) {
			//not using this check because not 100% efficient
			@SuppressWarnings("unused")
			boolean inScreen = stream.readByte() == 1;
		}else if(packetId == SCREEN_PACKET) {
			int displayMode = stream.readUnsignedByte();
			player.setScreenWidth(stream.readUnsignedShort());
			player.setScreenHeight(stream.readUnsignedShort());
			@SuppressWarnings("unused")
			boolean switchScreenMode = stream.readUnsignedByte() == 1;
			if(!player.hasStarted() || player.hasFinished() || displayMode == player.getDisplayMode() || !player.getInterfaceManager().containsInterface(742))
				return;
			player.setDisplayMode(displayMode);
			player.getInterfaceManager().removeAll();
			player.getInterfaceManager().sendInterfaces();
			player.getInterfaceManager().sendInterface(742);
		}else if(packetId == CLICK_PACKET) {
			int mouseHash = stream.readShortLE();
			int mouseButton =  mouseHash >> 15;
			int time = mouseHash - (mouseButton << 15); //time
			int positionHash = stream.readIntV2();
			int y = positionHash >> 16; //y;
			int x = positionHash - (y << 16); //x
			//mass click or stupid autoclicker, lets stop lagg
			if(time <= 1 || x < 0 || x > player.getScreenWidth() || y < 0 || y > player.getScreenHeight()) {
			//	player.getSession().getChannel().close();
				clicked = false;
				return;
			}
			clicked = true;
		}else if (packetId == DIALOGUE_CONTINUE_PACKET) {
			@SuppressWarnings("unused")
			int junk = stream.readShortLE();
			int interfaceHash = stream.readIntV1();
			int interfaceId = interfaceHash >> 16;
			if(Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(!player.isRunning() || !player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = interfaceHash - (interfaceId << 16);
			player.getDialogueManager().continueDialogue(interfaceId, componentId);
		}else if(packetId == ACTION_BUTTON1_PACKET || packetId == ACTION_BUTTON2_PACKET
				|| packetId == ACTION_BUTTON4_PACKET || packetId == ACTION_BUTTON5_PACKET
				|| packetId == ACTION_BUTTON6_PACKET || packetId == ACTION_BUTTON7_PACKET
				|| packetId == ACTION_BUTTON8_PACKET || packetId == ACTION_BUTTON9_PACKET) {
			if(!clicked) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			clicked = false;
			int interfaceId = stream.readUnsignedShort();
			if(Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(player.isDead() || !player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = stream.readUnsignedShort();
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			int slotId = packetId != ACTION_BUTTON2_PACKET ? stream.readUnsignedShort() : -1;
			if(slotId == 65535)
				slotId = -1;
			if (interfaceId == 182) {
				if(player.getInterfaceManager().containsInventoryInter())
					return;
				if(componentId == 6) 
					if(!player.hasFinished())
						player.logout();
			}else if(interfaceId == 192) {
				if(componentId == 7)
					player.getCombatDefinitions().switchShowCombatSpells();
				else if(componentId == 9)
					player.getCombatDefinitions().switchShowTeleportSkillSpells();
				else if(componentId == 11)
					player.getCombatDefinitions().switchShowMiscallaneousSpells();
				else if(componentId == 13)
					player.getCombatDefinitions().switchShowSkillSpells();
				else if (componentId >= 15 & componentId <= 17)
					player.getCombatDefinitions().setSortSpellBook(componentId-15);
				else
					Magic.processNormalSpell(player, componentId, packetId);
			}else if(interfaceId == 193) {
				if(componentId == 5)
					player.getCombatDefinitions().switchShowCombatSpells();
				else if(componentId == 7)
					player.getCombatDefinitions().switchShowTeleportSkillSpells();
				else if(componentId == 9)
					player.getCombatDefinitions().switchShowMiscallaneousSpells();
				else if (componentId >= 11 && componentId <= 13)
					player.getCombatDefinitions().setSortSpellBook(componentId-1);
			}else if(interfaceId == 430) {
				if(componentId == 5)
					player.getCombatDefinitions().switchShowCombatSpells();
				else if(componentId == 7)
					player.getCombatDefinitions().switchShowTeleportSkillSpells();
				else if (componentId >= 9 & componentId <= 11)
					player.getCombatDefinitions().setSortSpellBook(componentId-9);
			}else if(interfaceId == 261) {
				if(player.getInterfaceManager().containsInventoryInter())
					return;
				if(componentId == 3) {
					player.toogleRun(true);
				}else if(componentId == 16) {
					if(player.getInterfaceManager().containsScreenInter()) {
						player.getPackets().sendGameMessage("Please close the interface you have open before setting your graphic options.");
						return;
					}
					player.stopAll();
					player.getInterfaceManager().sendInterface(742);
				}else if (componentId == 4)
					player.switchAllowChatEffects();
				else if (componentId == 6)
					player.switchMouseButtons();
				else if(componentId == 18) {
					if(player.getInterfaceManager().containsScreenInter()) {
						player.getPackets().sendGameMessage("Please close the interface you have open before setting your audio options.");
						return;
					}
					player.stopAll();
					player.getInterfaceManager().sendInterface(743);
				}
			}else if(interfaceId == 271) {
				if(componentId == 7 || componentId == 6)
					player.getPrayer().switchPrayer(slotId);
				else if (componentId == 8 && player.getPrayer().isUsingQuickPrayer())
					player.getPrayer().switchSettingQuickPrayer();
			}else if(interfaceId == 320) {
				player.stopAll();
				boolean lvlup = false;
				int skillMenu = -1;
				switch(componentId) {
			case 125: //Attack
				skillMenu = 1;
				if(player.getTemporaryAttributtes().get("leveledUp[0]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 1);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 10);
				}
		        break;
		    case 126: //Strength
				skillMenu = 2;
				if(player.getTemporaryAttributtes().get("leveledUp[2]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 2);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 20);
				}
		        break;
		    case 127: //Defence
				skillMenu = 5;
				if(player.getTemporaryAttributtes().get("leveledUp[1]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 5);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 40);
				}
		        break;
		    case 128: //Ranged
				skillMenu = 3;
				if(player.getTemporaryAttributtes().get("leveledUp[4]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 3);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 30);
				}
		        break;
		    case 129: //Prayer
				if(player.getTemporaryAttributtes().get("leveledUp[5]") != Boolean.TRUE) {
					skillMenu = 7;
					player.getPackets().sendConfig(965, 7);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 60);
				}
		        break;
		    case 130: //Magic
				if(player.getTemporaryAttributtes().get("leveledUp[6]") != Boolean.TRUE) {
					skillMenu = 4;
					player.getPackets().sendConfig(965, 4);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 33);
				}
		        break;
		    case 131: //Runecrafting
				if(player.getTemporaryAttributtes().get("leveledUp[20]") != Boolean.TRUE) {
					skillMenu = 12;
					player.getPackets().sendConfig(965, 12);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 100);
				}
		        break;
		    case 132: //Construction
				skillMenu = 22;
				if(player.getTemporaryAttributtes().get("leveledUp[21]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 22);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 698);
				}
		        break;
		    case 133: //Hitpoints
				skillMenu = 6;
				if(player.getTemporaryAttributtes().get("leveledUp[3]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 6);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 50);
				}
		        break;
		    case 134: //Agility
				skillMenu = 8;
				if(player.getTemporaryAttributtes().get("leveledUp[16]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 8);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 65);
				}
		        break;
		    case 135: //Herblore
				skillMenu = 9;
				if(player.getTemporaryAttributtes().get("leveledUp[15]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 9);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 75);
				}
		        break;
		    case 136: //Thieving
				skillMenu = 10;
				if(player.getTemporaryAttributtes().get("leveledUp[17]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 10);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 80);
				}
		        break;
		    case 137: //Crafting
				skillMenu = 11;
				if(player.getTemporaryAttributtes().get("leveledUp[12]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 11);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 90);
				}
		        break;
		    case 138: //Fletching
				skillMenu = 19;
				if(player.getTemporaryAttributtes().get("leveledUp[9]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 19);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 665);
				}
		        break;
		    case 139: //Slayer
				skillMenu = 20;
				if(player.getTemporaryAttributtes().get("leveledUp[18]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 20);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 673);
				}
		        break;
		    case 140: //Hunter
				skillMenu = 23;
				if(player.getTemporaryAttributtes().get("leveledUp[22]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 23);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 689);
				}
		        break;
		    case 141: //Mining
				skillMenu = 13;
				if(player.getTemporaryAttributtes().get("leveledUp[14]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 13);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 110);
				}
		        break;
		    case 142: //Smithing
				skillMenu = 14;
				if(player.getTemporaryAttributtes().get("leveledUp[13]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 14);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 115);
				}
		        break;
		    case 143: //Fishing
				skillMenu = 15;
				if(player.getTemporaryAttributtes().get("leveledUp[10]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 15);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 120);
				}
		        break;
		    case 144: //Cooking
				skillMenu = 16;
				if(player.getTemporaryAttributtes().get("leveledUp[7]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 16);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 641);
				}
		        break;
		    case 145: //Firemaking
				skillMenu = 17;
				if(player.getTemporaryAttributtes().get("leveledUp[11]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 17);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 649);
				}
		        break;
		    case 146: //Woodcutting
				skillMenu = 18;
				if(player.getTemporaryAttributtes().get("leveledUp[8]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 18);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 660);
				}
		        break;
		    case 147: //Farming
				skillMenu = 21;
				if(player.getTemporaryAttributtes().get("leveledUp[19]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 21);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 681);
				}
		        break;
		    case 148: //Summoning
				skillMenu = 24;
				if(player.getTemporaryAttributtes().get("leveledUp[23]") != Boolean.TRUE) {
					player.getPackets().sendConfig(965, 24);
				} else {
					lvlup = true;
					player.getPackets().sendConfig(1230, 705);
				}
		        break;
			}
			player.getInterfaceManager().sendInterface(lvlup ? 741 : 499);
			for(int skill = 0; skill < 25; skill++)
				player.getTemporaryAttributtes().remove("leveledUp["+skill+"]");
			if(skillMenu != -1)
				player.getTemporaryAttributtes().put("skillMenu", skillMenu);
			player.getPackets().sendConfig(1179, 0); //stops flash
			}else if(interfaceId == 378) {
				if(componentId == 152 && !player.isRunning()) //click here to play
					player.run();
			}else if(interfaceId == 499) {
				int skillMenu = -1;
				if(player.getTemporaryAttributtes().get("skillMenu") != null)
					skillMenu = (Integer) player.getTemporaryAttributtes().get("skillMenu");
				switch(componentId) {
				case 10:
					player.getPackets().sendConfig(965, skillMenu);
					break;
				case 11:
					player.getPackets().sendConfig(965, 1024 + skillMenu);
					break;
				case 12:
					player.getPackets().sendConfig(965, 2048 + skillMenu);
					break;
				case 13:
					player.getPackets().sendConfig(965, 3072 + skillMenu);
					break;
				case 14:
					player.getPackets().sendConfig(965, 4096 + skillMenu);
					break;
				case 15:
					player.getPackets().sendConfig(965, 5120 + skillMenu);
					break;
				case 16:
					player.getPackets().sendConfig(965, 6144 + skillMenu);
					break;
				case 17:
					player.getPackets().sendConfig(965, 7168 + skillMenu);
					break;
				case 18:
					player.getPackets().sendConfig(965, 8192 + skillMenu);
					break;
				case 19:
					player.getPackets().sendConfig(965, 9216 + skillMenu);
					break;
				case 20:
					player.getPackets().sendConfig(965, 10240 + skillMenu);
					break;
				case 21:
					player.getPackets().sendConfig(965, 11264 + skillMenu);
					break;
				case 22:
					player.getPackets().sendConfig(965, 12288 + skillMenu);
					break;
				case 23:
					player.getPackets().sendConfig(965, 13312 + skillMenu);
					break;
				case 29: //close inter
					player.stopAll();
					break;
				}
			}else if (interfaceId == 382) {
				if(componentId == 18) {
					player.stopAll();
					final WorldObject ditch = (WorldObject) player.getTemporaryAttributtes().get("wildernessditch");
					if(ditch == null)
						return;
					player.addStopDelay(4);
					player.setNextAnimation(new Animation(6132));
					final WorldTile toTile = new WorldTile(player.getX(), ditch.getY()+2, ditch.getPlane());
					player.setNextForceMovement(new ForceMovement(new WorldTile(player), 1, toTile, 2, 0));
					final ObjectDefinitions objectDef = ditch.getDefinitions();
					WorldTasksManager.schedule(new WorldTask() {
						@Override
						public void run() {
							player.setNextWorldTile(toTile, false);
							player.setNextFaceWorldTile(new WorldTile(ditch.getCoordFaceX(objectDef.getSizeX(), objectDef.getSizeY(), ditch.getRotation())
									, ditch.getCoordFaceY(objectDef.getSizeX(),objectDef.getSizeY(), ditch.getRotation())
									, ditch.getPlane()));
							player.getControlerManager().startControler("Wilderness");
						}
					}, 2);
				}
			}else if(interfaceId == 387) {
				if(componentId == 53) {
					player.stopAll();
					player.getInterfaceManager().sendInterface(102);
				}else if(componentId == 56) {
					player.stopAll();
					player.getInterfaceManager().sendInterface(667);
					player.getInterfaceManager().sendInventoryInterface(670);
					player.getPackets().sendIComponentText(667,36, "Stab: " + player.getCombatDefinitions().getBonuses()[0]);
					player.getPackets().sendIComponentText(667,37, "Slash: " + player.getCombatDefinitions().getBonuses()[1]);
					player.getPackets().sendIComponentText(667,38, "Crush: " + player.getCombatDefinitions().getBonuses()[2]);
					player.getPackets().sendIComponentText(667,39, "Magic: " + player.getCombatDefinitions().getBonuses()[3]);
					player.getPackets().sendIComponentText(667,40, "Range: " + player.getCombatDefinitions().getBonuses()[4]);
					player.getPackets().sendIComponentText(667,41, "Stab: " + player.getCombatDefinitions().getBonuses()[5]);
					player.getPackets().sendIComponentText(667,42, "Slash: " + player.getCombatDefinitions().getBonuses()[6]);
					player.getPackets().sendIComponentText(667,43, "Crush: " + player.getCombatDefinitions().getBonuses()[7]);
					player.getPackets().sendIComponentText(667,44, "Magic: " + player.getCombatDefinitions().getBonuses()[8]);
					player.getPackets().sendIComponentText(667,45, "Range: " + player.getCombatDefinitions().getBonuses()[9]);
					player.getPackets().sendIComponentText(667,46, "Summoning: " + player.getCombatDefinitions().getBonuses()[10]);
					player.getPackets().sendIComponentText(667,48, "Strength: " + player.getCombatDefinitions().getBonuses()[14]);
					player.getPackets().sendIComponentText(667,49, "Ranged Str: " + player.getCombatDefinitions().getBonuses()[15]);
					player.getPackets().sendIComponentText(667,50, "Prayer: " + player.getCombatDefinitions().getBonuses()[16]);
					player.getPackets().sendIComponentText(667,51, "Magic Damage: " + player.getCombatDefinitions().getBonuses()[17]+"%");
				}else if(componentId == 63) {
					if(player.getInterfaceManager().containsScreenInter()) {
						player.getPackets().sendGameMessage("Please finish what you're doing before opening the price checker.");
						return;
					}
					player.stopAll();
					player.getInterfaceManager().sendInterface(206);
					player.getInterfaceManager().sendInventoryInterface(207);
				}
			}else if(interfaceId == 742) {
				if(componentId == 46) //close
					player.stopAll();
			}else if(interfaceId == 743) {
				if(componentId == 20) //close
					player.stopAll();
			}else if(interfaceId == 741) {
				if(componentId == 9) //close
					player.stopAll();
			}else if(interfaceId == 749) {
				if(componentId == 1) {
					if(packetId == 216) //activate
						player.getPrayer().switchQuickPrayers();
					else if(packetId == 19) //switch
						player.getPrayer().switchSettingQuickPrayer();
				}
			}else if (interfaceId == 750) {
				if(componentId == 1) {
					if(player.isResting()) {
						player.toogleRun(true);
						player.stopAll();
						return;
					}
					if(packetId == 216)
						player.toogleRun(true);
					else if (packetId == 19) {
						player.stopAll();
						player.setResting(true);
						player.setNextAnimation(new Animation(5713));
					}
				}
			}else if (interfaceId == 762) {
				if(componentId == 14)
					player.getBank().switchInsertItems();
				else if(componentId == 18)
					player.getBank().switchWithdrawNotes();
				else if (componentId == 20)
					player.getBank().depositAllInv();
				else if (componentId == 22)
					player.getBank().depositAllEquipment();
				else if (componentId == 49)
					player.getBank().sendBankName();
				else if (componentId == 81) {
					if(packetId == 216) 
						player.getBank().withdrawItem(slotId, 1);
					else if (packetId == 19)
						player.getBank().withdrawItem(slotId, 5);
					else if (packetId == 193)
						player.getBank().withdrawItem(slotId, 10);
					else if (packetId == 76)
						player.getBank().withdrawLastAmount(slotId);
					else if (packetId == 173) {
						player.getTemporaryAttributtes().put("bank_item_X_Slot", slotId);
						player.getTemporaryAttributtes().put("bank_isWithdraw", Boolean.TRUE);
						player.getPackets().sendRunScript(108, new Object[]{"Enter Amount:"});
					}
					else if (packetId == 89)
						player.getBank().withdrawItem(slotId, Integer.MAX_VALUE);
					else if (packetId == 221)
						player.getBank().withdrawItemButOne(slotId);
				}
			}else if(interfaceId == 763) {
				if(componentId == 0) {
					if(packetId == 216) 
						player.getBank().depositItem(slotId, 1);
					else if(packetId == 19) 
						player.getBank().depositItem(slotId, 5);
					else if(packetId == 193) 
						player.getBank().depositItem(slotId, 193);
					else if (packetId == 76)
						player.getBank().depositLastAmount(slotId);
					else if (packetId == 173) {
						player.getTemporaryAttributtes().put("bank_item_X_Slot", slotId);
						player.getTemporaryAttributtes().remove("bank_isWithdraw");
						player.getPackets().sendRunScript(108, new Object[]{"Enter Amount:"});
					}else if (packetId == 89)
						player.getBank().depositItem(slotId, Integer.MAX_VALUE);
				}
			}else if (interfaceId == 884) {
				if(componentId == 4)
					player.getCombatDefinitions().switchUsingSpecialAttack();
				else if (componentId >= 11 && componentId <= 14)
					player.getCombatDefinitions().setAttackStyle(componentId-11);
				else if (componentId == 15)
					player.getCombatDefinitions().switchAutoRelatie();
			}
			System.out.println("InterfaceId "+interfaceId+", componentId "+componentId+", slotId "+slotId+", PacketId: "+packetId);
		}else if (packetId == ENTER_INTEGER_PACKET) {
			if(!player.isRunning() || player.isDead())
				return;
			int value = stream.readInt();
			if(player.getInterfaceManager().containsInterface(762)
					&& player.getInterfaceManager().containsInterface(763)) {
				if(value < 0)
					return;
				Integer bank_item_X_Slot = (Integer) player.getTemporaryAttributtes().get("bank_item_X_Slot");
				if(bank_item_X_Slot == null)
					return;
				if(player.getTemporaryAttributtes().contains("bank_isWithdraw"))
					player.getBank().withdrawItem(bank_item_X_Slot, value);
				else
					player.getBank().depositItem(bank_item_X_Slot, value);
				player.getTemporaryAttributtes().remove("bank_item_X_Slot");
			}
		}else if (packetId == ITEM_OPTION1_PACKET) {
			if(!clicked) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			clicked = false;
			int interfaceId = stream.readUnsignedShort();
			if(player.isDead() || Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = stream.readUnsignedShort();
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			int itemId = stream.readUnsignedShort128();
			int slotId = stream.readUnsignedShortLE();
			if(interfaceId == 387 && componentId == 29) {
				if(slotId >= 14 || player.getInterfaceManager().containsInventoryInter())
					return;	
				Item item = player.getEquipment().getItem(slotId);
				if(item == null || item.getId() != itemId || !player.getInventory().addItem(item.getId(), item.getAmount()))
					return;
				player.getEquipment().getItems().set(slotId, null);
				player.getEquipment().refresh(slotId);
				player.getAppearence().generateAppearenceData();
			}
		}else if(packetId == WEAR_ITEM_PACKET) {
			if(!clicked) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			clicked = false;
			int interfaceId = stream.readUnsignedShort();
			
			if(player.isDead() || Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = stream.readUnsignedShort();
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			int itemId = stream.readUnsignedShortLE128();
			int slotId = stream.readUnsignedShortLE128();
			if(interfaceId == 149 && componentId == 0) {
				if(slotId >= 28 || player.getInterfaceManager().containsInventoryInter())
					return;
				Item item = player.getInventory().getItem(slotId);
				
				if(item == null || item.getId() != itemId || item.getDefinitions().isNoted() || !item.getDefinitions().isWearItem())
					return;
				int targetSlot = Equipment.getItemSlot(itemId);
				if(targetSlot == -1)
					return;
				player.stopAll();
				boolean isTwoHandedWeapon = targetSlot == 3 && Equipment.isTwoHandedWeapon(item);
				if(isTwoHandedWeapon && !player.getInventory().hasFreeSlots() && player.getEquipment().hasShield()) {
					player.getPackets().sendGameMessage("Not enough free space in your inventory.");
					return;
				}
				HashMap<Integer, Integer> requiriments = item.getDefinitions().getWearingSkillRequiriments();
				boolean hasRequiriments = true;
				if(requiriments != null) {
					for(int skillId : requiriments.keySet()) {
						if(skillId > 24 || skillId < 0)
							continue;
						int level = requiriments.get(skillId);
						if(level < 0 || level > 120)
							continue;
						if(player.getSkills().getLevelForXp(skillId) < level) {
							if(hasRequiriments)
								player.getPackets().sendGameMessage("You are not high enough level to use this item.");
							hasRequiriments = false;
							String name = Skills.SKILL_NAME[skillId].toLowerCase();
							player.getPackets().sendGameMessage("You need to have a"+ (name.startsWith("a") ? "n": "")+" "+name+" level of "+level+".");
						}
						
					}
				}
				if(!hasRequiriments)
					return;
				player.getInventory().deleteItem(slotId, item);
				if (targetSlot == 3) {
					if (isTwoHandedWeapon && player.getEquipment().getItem(5) != null) {
						if (!player.getInventory().addItem(
								player.getEquipment().getItem(5).getId(),
								player.getEquipment().getItem(5).getAmount())) {
							player.getInventory().addItem(itemId, item.getAmount());
							return;
						}
						player.getEquipment().getItems().set(5, null);
					}
				} else if (targetSlot == 5) {
					if (player.getEquipment().getItem(3) != null
							&& Equipment.isTwoHandedWeapon(player.getEquipment().getItem(3))) {
						if (!player.getInventory().addItem(
								player.getEquipment().getItem(3).getId(),
								player.getEquipment().getItem(3).getAmount())) {
							player.getInventory().addItem(itemId, item.getAmount());
							return;
						}
						player.getEquipment().getItems().set(3, null);
					}

				}
				if (player.getEquipment().getItem(targetSlot) != null
						&& (itemId != player.getEquipment().getItem(targetSlot)
								.getId() || !item.getDefinitions()
								.isStackable())) {
					player.getInventory().addItem(
							player.getEquipment().getItem(targetSlot)
									.getId(),
									player.getEquipment().getItem(targetSlot).getAmount());
					player.getEquipment().getItems().set(targetSlot, null);
				}
				int oldAmt = 0;
				if (player.getEquipment().getItem(targetSlot) != null) {
					oldAmt = player.getEquipment().getItem(targetSlot).getAmount();
				}

				Item item2 = new Item(itemId, oldAmt + item.getAmount());
				player.getEquipment().getItems().set(targetSlot, item2);
				player.getEquipment().refresh(targetSlot, targetSlot == 3 ? 5 : targetSlot == 3 ? 0 : 3);
				player.getAppearence().generateAppearenceData();
				player.getPackets().sendSound(2248, 0);
				player.getCombatDefinitions().desecreaseSpecialAttack(0);
			}
		} else if (packetId == ITEM_ON_OBJECT) {
			final int id = stream.readUnsignedShortLE();
			int itemId = stream.readUnsignedShortLE128();
			int slotId = stream.readUnsignedShortLE128();
			Item item = player.getInventory().getItem(slotId);
			System.out.println(id + " " + itemId + " " + slotId + " ");
 		} else if (packetId == ITEM_DROP_PACKET) {
			if(!clicked) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			clicked = false;
			int interfaceId = stream.readUnsignedShort();
			
			if(player.isDead() || Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = stream.readUnsignedShort();
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			int slotId = stream.readUnsignedShort128();
			int itemId = stream.readUnsignedShortLE();
			if(interfaceId == 149 && componentId == 0) {
				if(slotId >= 28 || player.getInterfaceManager().containsInventoryInter())
					return;
				Item item = player.getInventory().getItem(slotId);
				if(item == null || item.getId() != itemId)
					return;
				if(item.getDefinitions().isDestroyItem()) {
					player.getDialogueManager().startDialogue("DestroyItemOption", slotId, item);
					return;
				}
				player.getInventory().deleteItem(slotId, item);
				World.addGroundItem(item, new WorldTile(player), player, false, 180, true);
			}
		}else if(packetId == ITEM_TAKE_PACKET) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			long currentTime =  System.currentTimeMillis();
			if(player.getStopDelay() > currentTime || player.getFreezeDelay() >= currentTime)
				return;
			int y = stream.readShortLE128();
			final int id = stream.readShort128();
			int x = stream.readShortLE128();
			final WorldTile tile = new WorldTile(x, y, player.getPlane());
			final int regionId = tile.getRegionId();
			if(!player.getMapRegionsIds().contains(regionId))
				return;
			final FloorItem item = World.getRegion(regionId).getGroundItem(id, tile, player);
			if(item == null)
				return;
			player.stopAll(false);
			player.setCoordsEvent(new CoordsEvent(tile, new Runnable() {
				@Override
				public void run() {
					final FloorItem item = World.getRegion(regionId).getGroundItem(id, tile, player);
					if(item == null)
						return;
					player.setNextFaceWorldTile(tile);
					World.removeGroundItem(player, item);
				}
			},  1));
		}else if (packetId == SWITCH_INTERFACE_ITEM_PACKET) {
			if(!clicked) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			clicked = false;
			int interfaceId = stream.readUnsignedShort();
			if(Utils.getInterfaceDefinitionsSize() <= interfaceId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			int componentId = stream.readUnsignedShort();
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId) {
				//hack, or server error or client error
				//player.getSession().getChannel().close();
				return;
			}
			@SuppressWarnings("unused")
			int unknownByte = stream.readByteC();
			int toSlot = stream.readUnsignedShort128();
			int fromSlot = stream.readUnsignedShort128();
			if(interfaceId == 149 && componentId == 0) {
				if(toSlot >= player.getInventory().getItemsContainerSize() || fromSlot  >= player.getInventory().getItemsContainerSize()) {
					//hack, or server error or client error
					//player.getSession().getChannel().close();
					return;
				}
				player.getInventory().switchItem(fromSlot, toSlot);
			}
		}else if(packetId == SWITCH_DETAIL) {
			int hash = stream.readInt();
			if(hash != 1057001181) {
				//hack, or server error or client error
				player.getSession().getChannel().close();
				return;
			}
			//done loading region when switch detail or mapregion
		}else if (packetId == DONE_LOADING_REGION) {
			if(!player.clientHasLoadedMapRegion()) {
				//load objects and items here
				player.setClientHasLoadedMapRegion();
			}
			player.refreshSpawnedObjects();
			player.refreshSpawnedItems();
		}else if (packetId == WALKING_PACKET || packetId == MINI_WALKING_PACKET) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			long currentTime =  System.currentTimeMillis();
			if(player.getStopDelay() > currentTime)
				return;
			if(player.getFreezeDelay() >= currentTime) {
				player.getPackets().sendGameMessage("A magical force stops you from moving.");
				return;
			}
			if(packetId == MINI_WALKING_PACKET)
				length -= 18;
			int coordY = stream.readUnsignedShortLE();
			stream.read128Byte();
			int coordX = stream.readUnsignedShort();
			int steps = (length - 5) / 2;
			if(steps > 25)
				steps = 25;
			player.stopAll();
			if(player.isResting())
				return;
			if(player.addWalkSteps(coordX, coordY)) {
				for(int step = 0; step < steps; step++) {
					if(!player.addWalkSteps(coordX+stream.readByte(), coordY+stream.read128Byte())) {
						break;
					}
				}
			}
			if(player.getMining()) {
				player.setMining(false);
			}
		}else if (packetId == WEIRD_WALKING_PACKET) {
			/*if(!player.hasStarted() || player.hasFinished())
				return;
			int coordX = stream.readShort();
			int coordY = stream.readShort();
			player.resetWalkSteps();
			player.addWalkSteps(coordX, coordY);*/
		}else if (packetId == ATTACK_PLAYER) {
			try {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			if(!player.isCanPvp() || player.getStopDelay() > System.currentTimeMillis())
				return;
			@SuppressWarnings("unused")
			boolean unknown = stream.read128Byte() == 1;
			int playerIndex = stream.readUnsignedShort();
			Player p2 = World.getPlayers().get(playerIndex);
			if(p2 == null || p2.isDead() || p2.hasFinished() || !player.getMapRegionsIds().contains(p2.getRegionId()))
				return;
			if(!player.getControlerManager().canAttack(p2))
				return;
			if(!player.isCanPvp() || !p2.isCanPvp())  {
				player.getPackets().sendGameMessage("You can only attack players in a player-vs-player area.");
				return;
			}
			if(!p2.isAtMultiArea() || ! player.isAtMultiArea()) {
				if(player.getAttackedBy() != p2 && player.getAttackedByDelay() > System.currentTimeMillis()) {
					player.getPackets().sendGameMessage("You are already in combat.");
					return;
				}
				if(p2.getAttackedBy() != player && p2.getAttackedByDelay() > System.currentTimeMillis()) {
					if(p2.getAttackedBy() instanceof NPC) {
						p2.setAttackedBy(player); //changes enemy to player, player has priority over npc on single areas
					}else {
						player.getPackets().sendGameMessage("That player is already in combat.");
						return;
					}
				}
			}
			player.stopAll(false);
			player.getSkillExecutor().setSkill(new Combat(p2));
			}catch(Exception e) {
				e.printStackTrace();
			}
		}else if (packetId == ATTACK_NPC) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			int npcIndex = stream.readUnsignedShortLE();
			@SuppressWarnings("unused")
			boolean unknown = stream.read128Byte() == 1;
			NPC npc = World.getNPCs().get(npcIndex);
			if(npc == null || npc.isDead() || npc.hasFinished() || !player.getMapRegionsIds().contains(npc.getRegionId()) || !npc.getDefinitions().hasAttackOption())
				return;
			if(!player.getControlerManager().canAttack(player))
				return;
			if((!npc.isAtMultiArea() || ! player.isAtMultiArea()) && npc.getAttackedBy() != player && npc.getAttackedByDelay() > System.currentTimeMillis()) {
				player.getPackets().sendGameMessage("This npc is already in combat.");
				return;
			}
			if(!npc.isAtMultiArea() || ! player.isAtMultiArea()) {
				if(player.getAttackedBy() != npc && player.getAttackedByDelay() > System.currentTimeMillis()) {
					player.getPackets().sendGameMessage("You are already in combat.");
					return;
				}
				if(npc.getAttackedBy() != player && npc.getAttackedByDelay() > System.currentTimeMillis()) {
					player.getPackets().sendGameMessage("This npc is already in combat.");
					return;
				}
			}
			player.stopAll(false);
			player.getSkillExecutor().setSkill(new Combat(npc));
		}else if (packetId == INTERFACE_ON_PLAYER) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			@SuppressWarnings("unused")
			boolean unknown = stream.readByteC() == 1;
			@SuppressWarnings("unused")
			int junk2 = stream.readUnsignedShortLE();
			int interfaceHash = stream.readIntV2();
			int interfaceId = interfaceHash >> 16;
			int componentId = interfaceHash - (interfaceId << 16);
			if(Utils.getInterfaceDefinitionsSize() <= interfaceId)
				return;
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId)
				return;
			int playerIndex = stream.readUnsignedShort128();
			Player p2 = World.getPlayers().get(playerIndex);
			if(p2 == null || p2.isDead() || p2.hasFinished() || !player.getMapRegionsIds().contains(p2.getRegionId()))
				return;
			player.stopAll(false);
			switch(interfaceId) {
			case 192:
				switch(componentId) {
				case 25: //air strike
				case 28: //water strike
				case 30: //earth strike
				case 32: //fire strike
				case 34: //air bolt
				case 39: //water bolt
				case 42: //earth bolt
				case 45: //fire bolt
				case 49: //air blast
				case 52: //water blast
				case 58: //earth blast
				case 63: //fire blast
				case 70: //air wave
				case 73: //water wave
				case 77: //earth wave
				case 80: //fire wave
					if(Magic.checkCombatSpell(player, componentId, 1, false)) {
						player.setNextFaceWorldTile(new WorldTile(p2.getCoordFaceX(p2.getSize())
								, p2.getCoordFaceY(p2.getSize())
								, p2.getPlane()));
						if(!player.getControlerManager().canAttack(p2))
							return;
						if(!player.isCanPvp() || !p2.isCanPvp())  {
							player.getPackets().sendGameMessage("You can only attack players in a player-vs-player area.");
							return;
						}
						if(!p2.isAtMultiArea() || ! player.isAtMultiArea()) {
							if(player.getAttackedBy() != p2 && player.getAttackedByDelay() > System.currentTimeMillis()) {
								player.getPackets().sendGameMessage("That "+(player.getAttackedBy() instanceof Player ? "player" : "npc")+" is already in combat.");
								return;
							}
							if(p2.getAttackedBy() != player && p2.getAttackedByDelay() > System.currentTimeMillis()) {
								if(p2.getAttackedBy() instanceof NPC) {
									p2.setAttackedBy(player); //changes enemy to player, player has priority over npc on single areas
								}else {
									player.getPackets().sendGameMessage("That player is already in combat.");
									return;
								}
							}
						}
						player.getSkillExecutor().setSkill(new Combat(p2));
					}
					break;
				}
				break;
			}
		}else if (packetId == INTERFACE_ON_NPC) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			if(player.getStopDelay() > System.currentTimeMillis())
				return;
			@SuppressWarnings("unused")
			int junk2 = stream.readUnsignedShortLE();
			int npcIndex = stream.readUnsignedShort();
			@SuppressWarnings("unused")
			boolean unknown = stream.readByte128() == 1;
			int interfaceHash = stream.readIntV2();
			int interfaceId = interfaceHash >> 16;
			int componentId = interfaceHash - (interfaceId << 16);
			if(Utils.getInterfaceDefinitionsSize() <= interfaceId)
				return;
			if(!player.getInterfaceManager().containsInterface(interfaceId))
				return;
			if(componentId == 65535)
				componentId = -1;
			if(componentId != -1 && Utils.getInterfaceDefinitionsComponentsSize(interfaceId) <= componentId)
				return;
			NPC npc = World.getNPCs().get(npcIndex);
			if(npc == null || npc.isDead() || npc.hasFinished() || !player.getMapRegionsIds().contains(npc.getRegionId()))
				return;
			if(!npc.getDefinitions().hasAttackOption()) {
				player.getPackets().sendGameMessage("You can't attack this npc.");
				return;
			}
			player.stopAll(false);
			switch(interfaceId) {
			case 192:
				switch(componentId) {
				case 25: //air strike
				case 28: //water strike
				case 30: //earth strike
				case 32: //fire strike
				case 34: //air bolt
				case 39: //water bolt
				case 42: //earth bolt
				case 45: //fire bolt
				case 49: //air blast
				case 52: //water blast
				case 58: //earth blast
				case 63: //fire blast
				case 70: //air wave
				case 73: //water wave
				case 77: //earth wave
				case 80: //fire wave
					if(Magic.checkCombatSpell(player, componentId, 1, false)) {
						player.setNextFaceWorldTile(new WorldTile(npc.getCoordFaceX(npc.getSize())
								, npc.getCoordFaceY(npc.getSize())
								, npc.getPlane()));
						if(!player.getControlerManager().canAttack(npc))
							return;
						if(!npc.isAtMultiArea() || ! player.isAtMultiArea()) {
							if(player.getAttackedBy() != npc && player.getAttackedByDelay() > System.currentTimeMillis()) {
								player.getPackets().sendGameMessage("You are already in combat.");
								return;
							}
							if(npc.getAttackedBy() != player && npc.getAttackedByDelay() > System.currentTimeMillis()) {
								player.getPackets().sendGameMessage("This npc is already in combat.");
								return;
							}
						}
						player.getSkillExecutor().setSkill(new Combat(npc));
					}
					break;
				}
				break;
			}
		}else if (packetId == NPC_CLICK1_PACKET) {
			@SuppressWarnings("unused")
			boolean unknown = stream.read128Byte() == 1;
			int npcIndex = stream.readUnsignedShort128();
			final NPC npc = World.getNPCs().get(npcIndex);
			if(npc == null || npc.isDead() || npc.hasFinished() || !player.getMapRegionsIds().contains(npc.getRegionId()))
				return;
			player.stopAll(false);
			
			if(npc.getDefinitions().name.equalsIgnoreCase("banker")) {
				if(player.withinDistance(npc, 2)) {
					npc.setNextFaceWorldTile(new WorldTile(player.getCoordFaceX(player.getSize())
							, player.getCoordFaceY(player.getSize())
							, player.getPlane()));
					player.setNextFaceWorldTile(new WorldTile(npc.getCoordFaceX(npc.getSize())
							, npc.getCoordFaceY(npc.getSize())
							, npc.getPlane()));
					player.getDialogueManager().startDialogue("Banker", npc.getId());
				}
				return;
			}
			player.setCoordsEvent(new CoordsEvent(npc, new Runnable() {
				@Override
				public void run() {
					npc.setNextFaceWorldTile(new WorldTile(player.getCoordFaceX(player.getSize())
							, player.getCoordFaceY(player.getSize())
							, player.getPlane()));
					player.setNextFaceWorldTile(new WorldTile(npc.getCoordFaceX(npc.getSize())
							, npc.getCoordFaceY(npc.getSize())
							, npc.getPlane()));
					player.getPackets().sendGameMessage("Nothing interesting happens.");
					System.out.println("cliked 1 at npc id : "+npc.getId()+", "+npc.getX()+", "+npc.getY()+", "+npc.getPlane());
				}
			},  npc.getSize()));
		}else if (packetId == NPC_CLICK2_PACKET) {
			int npcIndex = stream.readUnsignedShort128();
			@SuppressWarnings("unused")
			boolean unknown = stream.readByte() == 1;
			final NPC npc = World.getNPCs().get(npcIndex);
			if(npc == null || npc.isDead() || npc.hasFinished() || !player.getMapRegionsIds().contains(npc.getRegionId()))
				return;
			player.stopAll(false);
			if(npc.getDefinitions().name.equalsIgnoreCase("banker")) {
				if(player.withinDistance(npc, 2)) {
					npc.setNextFaceWorldTile(new WorldTile(player.getCoordFaceX(player.getSize())
							, player.getCoordFaceY(player.getSize())
							, player.getPlane()));
					player.setNextFaceWorldTile(new WorldTile(npc.getCoordFaceX(npc.getSize())
							, npc.getCoordFaceY(npc.getSize())
							, npc.getPlane()));
					player.getBank().initBank();
				}
				return;
			}
			player.setCoordsEvent(new CoordsEvent(npc, new Runnable() {
				@Override
				public void run() {
					npc.setNextFaceWorldTile(new WorldTile(player.getCoordFaceX(player.getSize())
							, player.getCoordFaceY(player.getSize())
							, player.getPlane()));
					player.setNextFaceWorldTile(new WorldTile(npc.getCoordFaceX(npc.getSize())
							, npc.getCoordFaceY(npc.getSize())
							, npc.getPlane()));
					player.getPackets().sendGameMessage("Nothing interesting happens.");
					System.out.println("cliked 2 at npc id : "+npc.getId()+", "+npc.getX()+", "+npc.getY()+", "+npc.getPlane());
				}
			},  npc.getSize()));
		}else if(packetId == OBJECT_CLICK1_PACKET) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			long currentTime =  System.currentTimeMillis();
			if(player.getStopDelay() > currentTime || player.getFreezeDelay() >= currentTime)
				return;
			final int x = stream.readUnsignedShort();
			@SuppressWarnings("unused")
			int junk = stream.readUnsignedByte128();
			final int id = stream.readUnsignedShortLE();
			final int y = stream.readUnsignedShortLE();
			final WorldTile tile = new WorldTile(x, y, player.getPlane());
			int regionId = tile.getRegionId();
			if(!player.getMapRegionsIds().contains(regionId))
				return;
			WorldObject mapObject = World.getRegion(regionId).getObject(id, tile);
			if(mapObject == null)
				return;
			final WorldObject object = !player.isAtDynamicRegion() ? mapObject : new WorldObject(id, mapObject.getType(), mapObject.getRotation(), x, y, player.getPlane());
			player.stopAll(false);
			final ObjectDefinitions objectDef = object.getDefinitions();
			player.setCoordsEvent(new CoordsEvent(tile, new Runnable() {
				@Override
				public void run() {
					player.setNextFaceWorldTile(new WorldTile(object.getCoordFaceX(objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation())
							, object.getCoordFaceY(objectDef.getSizeX(),objectDef.getSizeY(), object.getRotation())
							, object.getPlane()));
					if(!player.getControlerManager().processObjectClick1(object))
						return;
					if((id >= 1276 && id <= 1278)
						|| id == 1280
						|| (id >= 1282 && id <= 1289) //dead trees
						|| id == 1291
							|| id == 3033
							|| id == 3034
							|| id == 3036
							|| id == 24168
							|| id == 38784
							|| id == 38785
							|| id == 38786
							|| id == 38787
							|| id == 38788
							|| id == 47598) //normal trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Normal_Tree));
					else if (id == 1281
							|| id == 3037
							|| id == 38731) //oak trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Oak_Tree));
					else if (id ==2210
							||id == 2372
							|| id == 38616
							|| id == 139
							|| id == 142) //willow trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Willow_Tree));
					else if (id == 1307) //maple trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Maple_Tree));
					else if (id == 1309
							|| id == 38787
							|| id == 38755) //yew trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Yew_Tree));
					else if (id == 46318
							|| id == 46320
							|| id == 46322
							|| id == 56324)
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Ivy));
					else if (id == 1306) //magic trees
						player.getSkillExecutor().setSkill(new Woodcutting(object, TreeDefinitions.Magic_Tree));
					else if (id == 2350 && (object.getX() == 3352 && object.getY() == 3417 && object.getPlane() == 0))
						player.useStairs(832, new WorldTile(3177, 5731, 0), 1, 2);
					else if (id == 2353 && (object.getX() == 3177 && object.getY() == 5730 && object.getPlane() == 0))
						player.useStairs(828, new WorldTile(3353, 3416, 0), 1, 2);
					else if (id == 47120) { //zaros altar
						
						//recharge if needed
						if(player.getSkills().getLevel(Skills.PRAYER) < player.getSkills().getLevelForXp(Skills.PRAYER)) {
							player.addStopDelay(12);
							player.setNextAnimation(new Animation(12563));
							player.getSkills().set(Skills.PRAYER, (int) (player.getSkills().getLevelForXp(Skills.PRAYER)*1.15));
						}
						player.getDialogueManager().startDialogue("ZarosAltar");
					}
					else if(id == 2476) {
						player.resetWalkSteps();
						player.setNextWorldTile(new WorldTile(3087, 9971, 0));
					}else if(id == 29355) {
						player.resetWalkSteps();
						player.setNextWorldTile(new WorldTile(3221, 3214, 0));
					}else if(id == 2507) {
						player.resetWalkSteps();
						player.setNextWorldTile(new WorldTile(3017, 10248, 0));
			
					}else if(id == 32015) {
						player.resetWalkSteps();
						player.setNextWorldTile(new WorldTile(3224, 3211, 0));
					}else if(id == 7272) {
						player.resetWalkSteps();
						player.setNextWorldTile(new WorldTile(3300, 3305, 0));
					}else if(id == 45210) {
						if(player.getX() == 3252) {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3253, player.getY(), 0));
						} else if(player.getX() == 3253) {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3252, player.getY(), 0));
						}
					}else if(id == 29316) {
						if(player.getY() == 9945) {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3105, 9944, 0));
						} else {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3105, 9945, 0));
						}
					}else if(id == 29315) {
						if(player.getY() == 9945) {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3106, 9944, 0));
						} else {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(3106, 9945, 0));
						}
					}else if(id == 29375) {
						if(player.getY() == 9969) {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(player.getX(), 9964, 0));
						} else {
							player.resetWalkSteps();
							player.setNextWorldTile(new WorldTile(player.getX(), 9969, 0));
						}
					}

					//////////////////SKILLS
					//MINING
					else if(id ==11936||id ==11937||id ==11938||id ==11960||id ==11691||id ==11692||id==11933||id ==11934||id ==11935||id ==11957||id ==11958||id ==11959||id ==37307||id ==37308||id ==37309||id ==11954||id ==11955||id==11956||id ==2311||id ==37304||id ==37305||id ==37306||id ==11930||id ==11931||id==11932||id ==37310||id ==37312||id ==11942||id ==11943||id ==11944||id ==11939||id ==11941||id==14860||id ==14859) {
						System.out.println("test");
						Mining.mine(player, Mining.type(id), x, y, 0);
					}
					//RUNECRAFTING
					else if(id == 2478) {
						Runecrafting.craft(player, "air");
					}else if(id == 2480) {
						Runecrafting.craft(player, "water");
					}else if(id == 2487) {
						Runecrafting.craft(player, "chaos");
					}else if(id == 2483) {
						Runecrafting.craft(player, "body");
					}else if(id == 2485) {
						Runecrafting.craft(player, "law");
					}else if(id == 2482) {
						Runecrafting.craft(player, "fire");
					}else if(id == 2486) {
						Runecrafting.craft(player, "nature");
					}else if(id == 2479) {
						Runecrafting.craft(player, "mind");
					}else if(id == 2481) {
						Runecrafting.craft(player, "earth");
					}else if(id == 2488) {
						Runecrafting.craft(player, "death");
					}else if(id == 2484) {
						Runecrafting.craft(player, "cosmic");
					}
					else if (id == 36776 && (object.getX() == 3204 && object.getY() == 3229 && object.getPlane() == 0)
							|| id == 36778 && (object.getX() == 3204 && object.getY() == 3229 && object.getPlane() == 2))
						player.useStairs(-1, new WorldTile(3205, 3228, 1), 0, 1);
					else if (id == 36773 && (object.getX() == 3204 && object.getY() == 3207 && object.getPlane() == 0)
							|| id == 36775 && (object.getX() == 3204 && object.getY() == 3207 && object.getPlane() == 2))
						player.useStairs(-1, new WorldTile(3205, 3209, 1), 0, 1);
					else if (id == 36777 && (object.getX() == 3204 && object.getY() == 3229 && object.getPlane() == 1))
						player.getDialogueManager().startDialogue("ClimbNoEmoteStairs", new WorldTile(3205, 3228, 2), new WorldTile(3205, 3228, 0));
					else if (id == 36774 && (object.getX() == 3204 && object.getY() == 3207 && object.getPlane() == 1))
						player.getDialogueManager().startDialogue("ClimbNoEmoteStairs", new WorldTile(3205, 3209, 2), new WorldTile(3205, 3209, 0));
					else if (id == 36786)
						player.getDialogueManager().startDialogue("Banker", 4907); 
					else if (id == 42425 && object.getX() == 3220 && object.getY() == 3222) { //zaros portal
						player.useStairs(10256, new WorldTile(3353, 3416, 0), 4, 5, "And you find yourself into a digsite.");
						player.addWalkSteps(3222, 3223, -1, false);
						player.getPackets().sendGameMessage("You examine portal and it aborves you...");
					}else if (id == 46500 && object.getX() == 3351 && object.getY() == 3415) { //zaros portal
						player.useStairs(-1, new WorldTile(Settings.RESPAWN_PLAYER_LOCATION.getX(), Settings.RESPAWN_PLAYER_LOCATION.getY(), Settings.RESPAWN_PLAYER_LOCATION.getPlane()), 2, 3, "You found your way back to home.");
						player.addWalkSteps(3351, 3415, -1, false);
						
					}else if (id == 1441) {//wild ditch
						player.getTemporaryAttributtes().put("wildernessditch", object);
						player.getInterfaceManager().sendInterface(382);
					}
					else {
						player.getPackets().sendGameMessage("Nothing interesting happens.");
					}
					System.out.println("cliked 1 at object id : "+id+", "+object.getX()+", "+object.getY()+", "+object.getPlane());
				}
				
			}, objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation()));
		}
			else if (packetId == OBJECT_CLICK2_PACKET) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			long currentTime =  System.currentTimeMillis();
			if(player.getStopDelay() > currentTime || player.getFreezeDelay() >= currentTime)
				return;
			@SuppressWarnings("unused")
			int junk = stream.readUnsigned128Byte();
			int y = stream.readUnsignedShort128();
			final int id = stream.readUnsignedShort();
			int x = stream.readUnsignedShort();
			final WorldTile tile = new WorldTile(x, y, player.getPlane());
			int regionId = tile.getRegionId();
			if(!player.getMapRegionsIds().contains(regionId))
				return;
			WorldObject mapObject = World.getRegion(regionId).getObject(id, tile);
			if(mapObject == null)
				return;
			final WorldObject object = !player.isAtDynamicRegion() ? mapObject : new WorldObject(id, mapObject.getType(), mapObject.getRotation(), x, y, player.getPlane());
			player.stopAll(false);
			final ObjectDefinitions objectDef = object.getDefinitions();
			player.setCoordsEvent(new CoordsEvent(tile, new Runnable() {
				@Override
				public void run() {
					player.setNextFaceWorldTile(new WorldTile(object.getCoordFaceX(objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation())
							, object.getCoordFaceY(objectDef.getSizeX(),objectDef.getSizeY(), object.getRotation())
							, object.getPlane()));
					if(!player.getControlerManager().processObjectClick2(object))
						return;
					if (id == 36786)
						player.getBank().initBank();
					else if (id == 36777 && (object.getX() == 3204 && object.getY() == 3229 && object.getPlane() == 1))
						player.useStairs(-1, new WorldTile(3205, 3228, 2), 0, 1);
					else if (id == 36774 && (object.getX() == 3204 && object.getY() == 3207 && object.getPlane() == 1))
						player.useStairs(-1, new WorldTile(3205, 3209, 2), 0, 1);
					else
						player.getPackets().sendGameMessage("Nothing interesting happens.");
					System.out.println("cliked 2 at object id : "+id+", "+object.getX()+", "+object.getY()+", "+object.getPlane());
				}
			}, objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation()));
		}else if (packetId == OBJECT_CLICK3_PACKET) {
			if(!player.hasStarted() || !player.clientHasLoadedMapRegion() || player.isDead())
				return;
			long currentTime =  System.currentTimeMillis();
			if(player.getStopDelay() > currentTime || player.getFreezeDelay() >= currentTime)
				return;
			int x = stream.readUnsignedShort();
			final int id = stream.readUnsignedShortLE128();
			int y = stream.readUnsignedShortLE128();
			@SuppressWarnings("unused")
			int junk = stream.readUnsignedByte();
			final WorldTile tile = new WorldTile(x, y, player.getPlane());
			int regionId = tile.getRegionId();
			if(!player.getMapRegionsIds().contains(regionId))
				return;
			WorldObject mapObject = World.getRegion(regionId).getObject(id, tile);
			if(mapObject == null)
				return;
			final WorldObject object = !player.isAtDynamicRegion() ? mapObject : new WorldObject(id, mapObject.getType(), mapObject.getRotation(), x, y, player.getPlane());
			player.stopAll(false);
			final ObjectDefinitions objectDef = object.getDefinitions();
			player.setCoordsEvent(new CoordsEvent(tile, new Runnable() {
				@Override
				public void run() {
					player.setNextFaceWorldTile(new WorldTile(object.getCoordFaceX(objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation())
							, object.getCoordFaceY(objectDef.getSizeX(),objectDef.getSizeY(), object.getRotation())
							, object.getPlane()));
					if(!player.getControlerManager().processObjectClick3(object))
						return;
					player.setNextFaceWorldTile(tile);
					 if (id == 36777 && (object.getX() == 3204 && object.getY() == 3229 && object.getPlane() == 1))
							player.useStairs(-1, new WorldTile(3205, 3228, 0), 0, 1);
					else if (id == 36774 && (object.getX() == 3204 && object.getY() == 3207 && object.getPlane() == 1))
							player.useStairs(-1, new WorldTile(3205, 3209, 0), 0, 1);
					else
						player.getPackets().sendGameMessage("Nothing interesting happens.");
					System.out.println("cliked 2 at object id : "+id+", "+object.getX()+", "+object.getY()+", "+object.getPlane());
				}
			}, objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation()));
		}else if(packetId == PUBLIC_CHAT_PACKET) {
			if(!player.hasStarted() || player.hasFinished())
				return;
			int effects = stream.readShort();
			player.setNextPublicChatMessage(new PublicChatMessage(Huffman.readEncryptedMessage(76, stream), effects));
		}else if (packetId == QUICK_CHAT_PACKET) {
			//just tells you which client script created packet
			@SuppressWarnings("unused")
			boolean secondClientScript = stream.readByte() == 1;//script 5059 or 5061
			int fileId = stream.readUnsignedShort();
			byte[] data = null;
			if(length > 3) {
				data = new byte[length-3];
				stream.readBytes(data);
			}
			if(fileId == 1)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.AGILITY)};
			else if (fileId == 8)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.ATTACK)};
			else if (fileId == 13)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.CONSTRUCTION)};
			else if (fileId == 16)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.COOKING)};
			else if (fileId == 23)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.CRAFTING)};
			else if (fileId == 30)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.DEFENCE)};
			else if (fileId == 34)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.FARMING)};
			else if (fileId == 41)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.FIREMAKING)};
			else if (fileId == 47)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.FISHING)};
			else if (fileId == 55)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.FLETCHING)};
			else if (fileId == 62)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.HERBLORE)};
			else if (fileId == 70)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.HITPOINTS)};
			else if (fileId == 74)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.HUNTER)};
			else if (fileId == 135)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.MAGIC)};
			else if (fileId == 127)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.MINING)};
			else if (fileId == 120)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.PRAYER)};
			else if (fileId == 116)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.RANGE)};
			else if (fileId == 111)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.RUNECRAFTING)};
			else if (fileId == 103)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.SLAYER)};
			else if (fileId == 96)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.SMITHING)};
			else if (fileId == 92)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.STRENGTH)};
			else if (fileId == 85)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.SUMMONING)};
			else if (fileId == 79)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.THIEVING)};
			else if (fileId == 142)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.WOODCUTTING)};
			else if (fileId == 990)
				data = new byte[] {(byte) player.getSkills().getLevelForXp(Skills.DUNGEONEERING)};
			player.setNextPublicChatMessage(new QuickChatMessage(fileId, data));
		}else if(packetId == COMMANDS_PACKET) {
			if(!player.isRunning())
				return;
			boolean clientCommand = stream.readUnsignedByte() == 1;
			String command = stream.readString().toLowerCase();
			String[] cmd = command.split(" ");
			if(clientCommand) {
				//unused atm
			}else{
				if(cmd[0].equals("shutdown")) {
					int delay = 60;
					if(cmd.length >= 2) {
						try {
							delay = Integer.valueOf(cmd[1]);
						}catch(NumberFormatException e) {
							 player.getPackets().sendPainelBoxMessage("Use: ::shutdown secondsDelay(IntegerValue)");
							 return;
						};
					}
					World.safeShutdown(false, delay);
				}else if(cmd[0].equals("npc")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::spawnnpc npcId");
						return;
					}
					try {
						World.spawnNPC(Integer.valueOf(cmd[1]), new WorldTile(player), -1, true);
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::spawnnpc npcId");
						return;
					}
				}else if(command.equals("spec")) {
					for(int i = 0; i < 10; i++)
					player.getCombatDefinitions().restoreSpecialAttack();
				}else if(cmd[0].equals("internpc")) {
					if(cmd.length < 4) {
						player.getPackets().sendPainelBoxMessage("Use: ::internpc interfaceId componentId npcId");
						return;
					}
					try {
						int interfaceId = Integer.valueOf(cmd[1]);
						int componentId = Integer.valueOf(cmd[2]);
						int npcId = Integer.valueOf(cmd[3]);
						player.getPackets().sendNPCOnIComponent(interfaceId, componentId, npcId);
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::internpc interfaceId componentId npcId");
					}
				}else if(cmd[0].equals("interface")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::inter interfaceId");
						return;
					}
					try {
						player.getInterfaceManager().sendInterface(Integer.valueOf(cmd[1]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::inter interfaceId");
					}
				}else if(cmd[0].equals("hidec")) {
					if(cmd.length < 4) {
						player.getPackets().sendPainelBoxMessage("Use: ::hidec interfaceid componentId hidden");
						return;
					}
					try {
						player.getPackets().sendHideIComponent(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]), Boolean.valueOf(cmd[3]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::hidec interfaceid componentId hidden");
					}
				}else if(cmd[0].equals("sound")) {
					int id = Integer.parseInt(cmd[1]);
					player.getPackets().sendSound(id, 0);
				}else if(cmd[0].equals("mining")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(3300, 3305, 0));
				}else if(cmd[0].equals("air")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2846, 4836, 0));
				}else if(cmd[0].equals("mind")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2784, 4843, 0));
				}else if(cmd[0].equals("water")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(3482, 4838, 0));
				}else if(cmd[0].equals("earth")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2657, 4830, 0));
				}else if(cmd[0].equals("fire")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2587, 4836, 0));
				}else if(cmd[0].equals("body")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2522, 4842, 0));
				}else if(cmd[0].equals("cosmic")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2142, 4836, 0));
				}else if(cmd[0].equals("chaos")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2269, 4840, 0));
				}else if(cmd[0].equals("nature")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2400, 4844, 0));
				}else if(cmd[0].equals("law")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2466, 4833, 0));
				}else if(cmd[0].equals("death")) {
					player.resetWalkSteps();
					player.setNextWorldTile(new WorldTile(2207, 4836, 0));
				}else if(cmd[0].equals("tele")) {
					if(cmd.length < 3) {
						player.getPackets().sendPainelBoxMessage("Use: ::tele coordX coordY");
						return;
					}
					try {
						player.setNextWorldTile(new WorldTile(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]), cmd.length >= 4 ? Integer.valueOf(cmd[3]) : player.getPlane()));
						player.resetWalkSteps();

					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::tele coordX coordY plane");
					}
				}else if(cmd[0].equals("face")) {
						player.getPackets().sendPainelBoxMessage("face is " + player.getDirection());
				}else if(cmd[0].equals("tonpc")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::tonpc id(-1 for player)");
						return;
					}
					try {
						player.getAppearence().transformIntoNPC(Integer.valueOf(cmd[1]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::tonpc id(-1 for player)");
					}
				}else if(cmd[0].equals("objectf")) {
					System.out.println("test");
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use objectf id face");
						return;
					}
					try {
						int objectId = Integer.valueOf(cmd[1]);
						int objectFace = Integer.parseInt(cmd[2]);
						for(int regionId : player.getMapRegionsIds()) {
							CopyOnWriteArrayList<Integer> playersIndexes = World.getRegion(regionId).getPlayerIndexes();
							if(playersIndexes == null)
								continue;
							for(Integer playerIndex : playersIndexes) {
								Player p2 = World.getPlayers().get(playerIndex);
								if(p2 == null
										|| !p2.hasStarted()
										|| p2.hasFinished()
										|| !p2.withinDistance(p2))
									continue;
								p2.getPackets().sendSpawnedObject(new WorldObject(objectId, 10, objectFace, player.getX(), player.getY(), player.getPlane()));
							}
						}
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::spawnobject id");
					}
					
					player.getAppearence().setRenderEmote(-1);
				}else if(cmd[0].equals("object")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::spawnobject id");
						return;
					}
					try {
						int objectId = Integer.valueOf(cmd[1]);
						int face = Integer.valueOf(cmd[2]);
						for(int regionId : player.getMapRegionsIds()) {
							CopyOnWriteArrayList<Integer> playersIndexes = World.getRegion(regionId).getPlayerIndexes();
							if(playersIndexes == null)
								continue;
							for(Integer playerIndex : playersIndexes) {
								Player p2 = World.getPlayers().get(playerIndex);
								if(p2 == null
										|| !p2.hasStarted()
										|| p2.hasFinished()
										|| !p2.withinDistance(p2))
									continue;
								p2.getPackets().sendSpawnedObject(new WorldObject(objectId, 10, face, player.getX(), player.getY(), player.getPlane()));
							}
						}
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::spawnobject id");
					}
					
					player.getAppearence().setRenderEmote(-1);
				}else if(cmd[0].equals("remote")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::remote id");
						return;
					}
					try {
						player.getAppearence().setRenderEmote(Integer.valueOf(cmd[1]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::remote id");
					}
				}else if(cmd[0].equals("gfxtile")) {
					if(cmd.length < 5) {
						player.getPackets().sendPainelBoxMessage("Use: ::gfxtile id x y plane");
						return;
					}
					try {
						player.getPackets().sendGraphics(new Graphics(Integer.valueOf(cmd[1])), new WorldTile(Integer.valueOf(cmd[2]), Integer.valueOf(cmd[3]), Integer.valueOf(cmd[4])));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::gfxtile id x y plane");
					}
				}else if(cmd[0].equals("spellbook")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::spellbook id");
						return;
					}
					try {
						player.getCombatDefinitions().setSpellBook(Integer.valueOf(cmd[1]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::spellbook id");
					}
				}else if(cmd[0].equals("item")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::item id (optional:amount)");
						return;
					}
					try {
						player.getInventory().addItem(Integer.valueOf(cmd[1]), cmd.length >= 3 ? Integer.valueOf(cmd[2]) : 1);
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::item id (optional:amount)");
					}
				}else if(cmd[0].equals("config")) {
					if(cmd.length < 3) {
						player.getPackets().sendPainelBoxMessage("Use: ::config id value");
						return;
					}
					try {
						player.getPackets().sendConfig(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::config id value");
					}
				}else if(cmd[0].equals("bconfig")) {
					if(cmd.length < 3) {
						player.getPackets().sendPainelBoxMessage("Use: ::bconfig id value");
						return;
					}
					try {
						player.getPackets().sendButtonConfig(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::bconfig id value");
					}
				}else if(cmd[0].equals("anim")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::emote id");
						return;
					}
					try {
						player.setNextAnimation(new Animation(Integer.valueOf(cmd[1])));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::emote id");
					}
				}else if(cmd[0].equals("gfx")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::gfx id");
						return;
					}
					try {
						player.setNextGraphics(new Graphics(Integer.valueOf(cmd[1])));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::gfx id");
					}
				}else if(command.equals("testdung")) {
					player.getControlerManager().startControler("Dungeonnering", new WorldTile(4000, 4000, 0));
				}else if(command.equals("resetinv")) {
					player.getInventory().reset();
				}else if(command.equals("bank")) {
					player.getBank().initBank();
				}else if(command.equals("coords")) {
					player.getPackets().sendPainelBoxMessage("Coords: "+player.getX()+", "+player.getY()+", "+player.getPlane()+", regionId: "+player.getRegionId()+", rx: "+player.getRegionX()+", ry: "+player.getRegionY());
				}else if(cmd[0].equals("master")) {
					if(cmd.length < 2) {
						for(int skill = 0; skill < 25; skill++)
							player.getSkills().addXp(skill, Skills.MAXIMUM_EXP);
						return;
					}
					try {
						player.getSkills().addXp(Integer.valueOf(cmd[1]), Skills.MAXIMUM_EXP);
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::master skill");
					}
				}else if(cmd[0].equals("rights")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::rights value");
						return;
					}
					try {
						player.setRights(Integer.valueOf(cmd[1]));
						player.finish();
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::rights value");
					}
				}else if(cmd[0].equals("trygfx")) {
					if(cmd.length < 3) {
						player.getPackets().sendPainelBoxMessage("Use: ::trygfx fromid toid");
						return;
					}
					try {
						final int fromid = Integer.valueOf(cmd[1]);
						final int toid = Integer.valueOf(cmd[2]);
						if(toid < 0 || fromid < 0)
							player.getPackets().sendGameMessage("ERROR FROM ID OR TO ID LOWER THAN 0.");
						else if(toid < fromid)
							player.getPackets().sendGameMessage("ERROR FROM ID LOWER THAN TO ID");
						else {
							player.getPackets().sendGameMessage("Starting in 2seconds from id "+fromid+", to id"+toid+".");
							player.getPackets().sendGameMessage("Logout to cancel or wait till end.");
							WorldTasksManager.schedule(new WorldTask() {

								int id = fromid;
								@Override
								public void run() {
								if(player.hasFinished() || toid < id) {
									stop();
									return;
								}
								player.setNextGraphics(new Graphics(id++));								
								}
								
							}, 2, 2);
						}
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::trygfx fromid toid");
					}
				}else if(cmd[0].equals("hit")) {
					if(cmd.length < 2) {
						player.getPackets().sendPainelBoxMessage("Use: ::hit ammounthits");
						return;
					}
					try {
						int loop =  Integer.valueOf(cmd[1]);
						if(loop < 0) //prevent deadlock
							return;
						for(int i = 0; i < loop; i++)
							player.applyHit(new Hit(player, Utils.getRandom(100)+1, Math.random() >= 0.5 ? HitLook.Melee_Hit : HitLook.Absorve_Hit));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::hit ammounthits");
					}
				}else if(cmd[0].equals("walk")) {
					if(cmd.length < 3) {
						player.getPackets().sendPainelBoxMessage("Use: ::walk nindex offsetX offsetY");
						return;
					}
					try {
						/*NPC n = World.getNPCs().get(Integer.valueOf(cmd[1]));
						if(n == null) {
							player.getPackets().sendPainelBoxMessage("n is null.");
							return;
						}
						n.resetWalkSteps();
						n.addWalkSteps(n.getX()+Integer.valueOf(cmd[2]), n.getY()+Integer.valueOf(cmd[3]));*/
						player.resetWalkSteps();
						player.addWalkSteps(player.getX()+Integer.valueOf(cmd[1]), player.getY()+Integer.valueOf(cmd[2]));
					}catch(NumberFormatException e) {
						player.getPackets().sendPainelBoxMessage("Use: ::walk nindex offsetX offsetY");
					}
				}
			}
		}else{
			Logger.log(this, "Missing packet "+packetId+", expected size: "+length+", actual size: "+PACKET_SIZES[packetId]);
		}
	}
	
	public Player getPlayer() {
		return player;
	}

}
